<% @category = @page.category %>
<h3 class="page-header">
  <%= page_title %> <small><%= @page.hint.presence || @category.page_hint %></small>
</h3>

<%= simple_form_for(@page, url: @page.new_record? ? cip_admin_pages_path : cip_admin_page_path(@page)) do |f| %>
  <div class="row">
    <div class="col-md-7">
      <%= f.error_messages %>

      <%= f.input :category_id, as: :hidden %>

      <%= f.input :title %>
      <%= f.input :author, placeholder: Cip::Page::DEFAULT_AUTHOR %>

      <div class="form-group file optional page_avatar">
        <label class="file optional control-label" for="page_avatar">预览图</label>
        <a class="text-muted" href="<%= @page.avatar.url(:original) %>" target="_blank">点击查看原图</a>
        <label class="clear-file pull-right" for="cip_page__destroy_avatar">
          <%= f.check_box :_destroy_avatar %>
          删除预览图
        </label>
        <%= f.file_field :avatar, style: "display: none;" %>
        <div id="cip_page_avatar_thumb" class="dropzone dz-clickable">
          <%= image_tag(@page.avatar.url(:thumb)) %>
          <span class="text-muted dz-message">点击或拖拽文件到此处更换预览图</span>
        </div>
      </div>

      <%= f.input :created_at, as: :datetime_picker %>
      <%= f.input :forwarding_type, collection: Cip::Page::FORWARDING_TYPES, as: :radio_buttons %>

    </div>
    <div class="col-md-12">

      <div id="tab-normal" class="cip_page_forwarding_type-tabs">
        <%= f.input :content, input_html: { class: "ckeditor", rows: 8 }, label: false %>
      </div>

    </div>
    <div class="col-md-7">

      <div id="tab-page" class="cip_page_forwarding_type-tabs">
        <%= f.input :forwarding_page_id, collection: Cip::Page.all, input_html: { class: 'select2' }, label: false %>
      </div>

      <div id="tab-category" class="cip_page_forwarding_type-tabs">
        <%= f.input :forwarding_category_id, collection: Cip::Category.all, input_html: { class: 'select2' }, label: false %>
      </div>

      <div id="tab-url" class="cip_page_forwarding_type-tabs">
        <%= f.input :forwarding_url, label: false %>
      </div>

      <% if superuser? %>
        <h4>网站结构信息，请勿随意修改</h4>

        <%= f.input :slug %>
        <%= f.input :position %>
        <%= f.input :category_id %>
        <%= f.input :view_count %>
        <%= f.input :disabled %>
        <%= f.input :hint %>
        <%= f.input :template %>
      <% end %>

      <% if @category.page_metadata_struct %>
        <h4>更多信息</h4>

        <%= f.fields_for :metadata, OpenStruct.new(@page.metadata) do |ff| %>
          <% @category.page_metadata_struct.each do |key, options| %>
            <%= ff.input key, { required: false }.merge(options.symbolize_keys!) %>
          <% end %>
        <% end %>
      <% end %>

      <%= f.button :submit, "保存", class: "btn-primary pull-right" %>
    </div>
  </div>
<% end %>

<script type="text/javascript">
  $("#cip_page_avatar").dropzone({
    preview: $("#cip_page_avatar_thumb")
  });

  var $forwarding_type_radios = $(".cip_page_forwarding_type input[type=radio]");
  var $forwarding_type_tabs = $(".cip_page_forwarding_type-tabs");

  function change_forwarding_type() {
    $forwarding_type_tabs.hide();
    var forwarding_type = $forwarding_type_radios.filter(":checked").val();
    if (forwarding_type) $("#tab-" + forwarding_type).show();
  }
  $forwarding_type_radios.change(change_forwarding_type);

  function init_change_forwarding_type() {
    if ($("#cip_page_forwarding_page_id.initialized, #cip_page_forwarding_category_id.initialized, #cip_page_content.initialized").length === 3) {
      change_forwarding_type();
    } else {
      setTimeout(init_change_forwarding_type, 100);
    }
  }

  init_change_forwarding_type();
</script>
